/*
 * configuracion.c
 *
 *  Created on: 22/04/2010
 *      Author: teledhil
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "configuracion.h"
#include "comandos.h"

#define MAX_LONG_WORD 100

FILE *configuracion_abrir(char *nombre) {

	return fopen(nombre, "r");
}

int configuracion_cerrar(FILE *stream) {

	fclose(stream);

	return 0;
}

int configuracion_eval(FILE *stream) {

	char argumento[MAX_LONG_WORD];
	char opcion[MAX_LONG_WORD];

	while (fscanf(stream, "%s", argumento) > 0) {
		if ((strcmp("set", argumento) == 0)) {
			//fprintf(stderr, "SET!\n");
			if (fscanf(stream,"%s",opcion)!=0) {
				//rowebcache [url]
				if ((strcmp("rowebcache", opcion) == 0)) {
					//fprintf(stderr,"%s!\n",opcion);
					if (fscanf(stream,"%s",opcion)!=0) {
						//fprintf(stderr,"\t%s\n",opcion);
						if(r_set_rowebcache(opcion)<0) {
							fprintf(stderr,"Error ajustando parametro rowebcache\n");
							fprintf(stderr,"\tValor %s incorrecto\n",opcion);
							return -1;
						}
					} else {
						//r_show_rowebcache();
						fprintf(stderr,"Error ajustando parametro rowebcache\n");
						return -1;
					}
					//localport [port]
				} else if ((strcmp("localport", opcion) == 0)) {
					//fprintf(stderr,"%s!\n",opcion);
					if (fscanf(stream,"%s",opcion)!=0) {
						//fprintf(stderr,"\t%s\n",opcion);
						if(r_set_localport(atoi(opcion))<0) {
							fprintf(stderr,"Error ajustando parametro localport\n");
							fprintf(stderr,"\tValor %s incorrecto\n",opcion);
							return -1;
						}
					} else {
						//r_show_localport();
						fprintf(stderr,"Error ajustando parametro localport\n");
						return -1;
					}
					//sharepath [path]
				} else if ((strcmp("sharepath", opcion) == 0)) {
					//fprintf(stderr,"%s!\n",opcion);
					if (fscanf(stream,"%s",opcion)!=0) {
						//fprintf(stderr,"\t%s\n",opcion);
						if(r_set_sharepath(opcion)<0) {
							fprintf(stderr,"Error ajustando parametro sharepath\n");
							fprintf(stderr,"\tValor %s incorrecto\n",opcion);
							return -1;
						}
					} else {
						//r_show_sharepath();
						fprintf(stderr,"Error ajustando parametro sharepath\n");
						return -1;
					}
					//incomingpath [path]
				} else if ((strcmp("incomingpath", opcion) == 0)) {
					//fprintf(stderr,"%s!\n",opcion);
					if (fscanf(stream,"%s",opcion)!=0) {
						//fprintf(stderr,"\t%s\n",opcion);
						if(r_set_incomingpath(opcion)<0) {
							fprintf(stderr,"Error ajustando parametro incomingpath\n");
							fprintf(stderr,"\tValor %s incorrecto\n",opcion);
							return -1;
						}
					} else {
						//r_show_incomingpath();
						fprintf(stderr,"Error ajustando parametro incomingpath\n");
						return -1;
					}
					//downloadpath [path]
				} else if ((strcmp("downloadpath", opcion) == 0)) {
					//fprintf(stderr,"%s!\n",opcion);
					if (fscanf(stream,"%s",opcion)!=0) {
						//fprintf(stderr,"\t%s\n",opcion);
						if(r_set_downloadpath(opcion)<0) {
							fprintf(stderr,"Error ajustando parametro downloadpath\n");
							fprintf(stderr,"\tValor %s incorrecto\n",opcion);
							return -1;
						}
					} else {
						//r_show_downloadpath();
						fprintf(stderr,"Error ajustando parametro downloadpath\n");
						return -1;
					}

				} else {
					fprintf(stderr, "Variable %s desconocida\n", opcion);
					return -1;
				}
			} else {
				//r_set();
				r_show_rowebcache();
				r_show_localport();
				r_show_sharepath();
				r_show_incomingpath();
				r_show_downloadpath();
				return -1;
			}

		} else {
			fprintf(stderr, "Comando %s desconocido\n", argumento);
			return -1;
		}

		//Borra las variables para la siguiente iteracion
		//memset(argumento,'\0',MAX_LONG_WORD);
		//memset(opcion,'\0',MAX_LONG_WORD);

	}

	return 0;
}


int configuracion_defecto() {

	r_set_rowebcache("127.0.0.1");
	r_set_localport(1138);
	r_set_sharepath("/var/www/");
	r_set_incomingpath("/media/incoming/");
	r_set_downloadpath("/media/Download");


	return 0;
}
